<?php

namespace App\Http\Controllers\Api\Oauth;

use App\Core\Utils\TwHelper\Environment;
use App\Http\Controllers\Api\Controller as BaseController;
use Illuminate\Http\Request;
use App\Models\Oauth\UserModel;
use App\Models\Club\MemberModel;
use App\Services\Oauth\LoginService;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cookie;
use App\Models\Oauth\CodeModel;
use App\Models\Oauth\AuthorizeModel;
use App\Services\Oauth\Oauth2\Oauthor;
use App\Services\Oauth\Oauth2\AccessToken;
use App\Services\Oauth\User;
use App\Http\Resources\Base\UserResource;
use App\Exceptions\Response\Service\ServiceCode;
use App\Exceptions\Response\Service\ServiceException;
use App\Services\Oauth\Oauth2\App;
use App\Services\Oauth\RegistService;
use App\Services\Oauth\UserService;
use App\Core\Utils\TwHelper\CommFunction;
use App\Models\Oauth\DistrictModel;
use App\Services\Oauth\OauthorService;
use App\Services\Utils\TencentYun\ImageService;
use TencentYun\Image\ImageV2;


/**
 * 手机短信相关api
 */
class ApiSmsController extends BaseController
{

    /**
     * 发送短信验证码(手机号未使用)
     * needVerify  是否需要验证 0:否 1:需要
     *
     */
    public function sendSmsMobileNotUsed(Request $request)
    {
        $this->validate($request, [
            'mobile' => 'required',
            'needVerify' => 'required',
        ]);
        //手机号的国家码，中国：86
        $nationCode = $request->input('nationCode', 86);
        $mobile = $request->input('mobile', '');
        $needVerify = $request->input('needVerify', '');
        $mobileUsedVerify = $request->input('mobileUsedVerify', '');
        //是否进行验证码验证
        if ($needVerify) {
            //需要验证码验证
            $verifyCode = $request->input('code', '');
            if (!$verifyCode || !captcha_check($verifyCode)) {
                throw new ServiceException(ServiceCode::SERVICE_OAUTH_VERIFY_CODE_WRONG, '', []);
            }
        }

        //验证手机号码是否已经使用，已使用就报异常
        $userService = new UserService();
        $id = $userService->checkMobile($mobile);
        if ($id) {
            throw new ServiceException(ServiceCode::SERVICE_OAUTH_MOBILE_IS_USERD, '', []);
        }

        $registService = new RegistService('', $mobile, '', '');
        if ($registService->sendSmsCode($nationCode)) {
            $data['status'] = 1;
            return $this->response->setResponseData($data)->getResponse();
        }
    }

    /**
     * 发送短信验证码(手机号已使用)
     * needVerify  是否需要验证 0:否 1:需要
     *
     */
    public function sendSmsMobileUsed(Request $request)
    {
        $this->validate($request, [
            'mobile' => 'required',
            'needVerify' => 'required',
        ]);
        //手机号的国家码，中国：86
        $nationCode = $request->input('nationCode', 86);
        $mobile = $request->input('mobile', '');
        $needVerify = $request->input('needVerify', '');
        $mobileUsedVerify = $request->input('mobileUsedVerify', '');
        //是否进行验证码验证
        if ($needVerify) {
            //需要验证码验证
            $verifyCode = $request->input('code', '');
            if (!$verifyCode || !captcha_check($verifyCode)) {
                throw new ServiceException(ServiceCode::SERVICE_OAUTH_VERIFY_CODE_WRONG, '', []);
            }
        }

        //验证手机号码是否已经使用，未使用就报异常
        $userService = new UserService();
        $id = $userService->checkMobile($mobile);
        if (!$id) {
            throw new ServiceException(ServiceCode::SERVICE_OAUTH_MOBILE_IS_NOT_USERD, '', []);
        }

        $registService = new RegistService('', $mobile, '', '');
        if ($registService->sendSmsCode($nationCode)) {
            $data['status'] = 1;
            return $this->response->setResponseData($data)->getResponse();
        }
    }

    /**
     * 验证短信验证码
     */
    public function checkSms(Request $request)
    {
        $this->validate($request, [
            'mobile' => 'required',
            'code' => 'required'
        ]);
        //手机号的国家码，中国：86
        $nationCode = $request->input('nationCode', 86);
        $mobile = $request->input('mobile', '');
        //短信验证码
        $code = $request->input('code', '');
        $registService = new RegistService('', $mobile, '', $code);
        //验证手机格式
        $registService->checkMobileFormat($nationCode);
        $userService = new UserService();
        //验证短信验证码
        $registService->checkSmsCode();
        $data['status'] = 1;
        return $this->response->setResponseData($data)->getResponse();
    }

}